package com.wsc.castle.ui.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class HoverButton extends Button {

	private float size = -1;
	private OnTouchListener touchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Button button = (Button) v;
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (size == -1)
					size = button.getTextSize();
				button.setTextSize(pixelsToSp(size + 6));
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				button.setTextSize(pixelsToSp(size));
			}
			return false;
		}
	};

	public HoverButton(Context context) {
		super(context);
		init();
	}

	public HoverButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public HoverButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		setOnTouchListener(touchListener);
	}

	// public float spToPixels(Context context, int i) {
	// float scaledDensity =
	// context.getResources().getDisplayMetrics().scaledDensity;
	// return i * scaledDensity;
	// }

	public float pixelsToSp(float px) {
		float scaledDensity = getContext().getResources().getDisplayMetrics().scaledDensity;
		return px / scaledDensity;
	}
}
